package com.linkstec.musiccloud.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.linkstec.musiccloud.pojo.ListSongUser;
import com.linkstec.musiccloud.pojo.User;
import com.linkstec.musiccloud.service.SonglistService;
import com.linkstec.musiccloud.vo.SongListVos;

@Controller
@RequestMapping("songList")
public class SongListController {

	@Autowired
	private SonglistService songlistService;

	/**
	 * @Description:TODO 分类查看歌单
	 * @author: shuaibizhe
	 * @date: 2018年11月23日 下午7:42:25
	 * @return
	 */
	@RequestMapping(value = "selectSongListByType")
	public ModelAndView selectSongListByType(ModelAndView mv) {
		List<SongListVos> list = null;
		try {
			list = songlistService.selectSongListByType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("list", list);
		mv.setViewName("shows");
		return mv;
	}

	/**     
	 * @Description:TODO 检查歌单中是否存在歌曲
	 * @author: shuaibizhe
	 * @date:   2018年11月25日 下午7:28:46    
	 * @param songId
	 * @param listId
	 * @param request
	 * @return      
	 */  
	@RequestMapping(value ="songIntoListCheck")
	@ResponseBody
	public String songIntoListCheck(String songId, int listId, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		User user = (User) request.getSession().getAttribute("loginUser");
		ListSongUser listSongUser = null;
		int songId1 = Integer.parseInt(songId);
		try {
			listSongUser = songlistService.songIntoListCheck(songId1,listId,user.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (listSongUser==null) {
			json.put("msg", 1);
		}else {
			json.put("msg", "该歌单已有此首歌");
		}
		return json.toString();
	}
	
	/**     
	 * @Description:TODO 插入到歌单
	 * @author: shuaibizhe
	 * @date:   2018年11月25日 下午7:30:57    
	 * @return      
	 */  
	@RequestMapping(value="songIntoList")
	public String songIntoList(String songId, int listId,HttpServletRequest request){
		int songId1 = Integer.parseInt(songId);
		User user = (User) request.getSession().getAttribute("loginUser");
		try {
			songlistService.songIntoList(songId1,listId,user.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/song/Comment?songId="+songId+"&type="+0;
	}
	
	
}
